package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.common.service.IApplicationInfoService;
import com.block.db.entity.ApplicationInfo;
import com.block.db.mapper.ApplicationInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 应用包管理 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-05
 */
@Service
public class ApplicationInfoServiceImpl extends ServiceImpl<ApplicationInfoMapper, ApplicationInfo> implements IApplicationInfoService {

    @Resource
    private RedisTemplate<String, ApplicationInfo> redisApplicationInfoTemplate;

    private  static  final  String APPINFO_KEY_PREFIX="APPINFO:APPTYPE:";
    private  static  final  Long APPINFO_KEY_TIME_OUT=4L;

    @Override
    public ApplicationInfo getAppInfoByAppCode(String appCode){
        String redisKey = APPINFO_KEY_PREFIX+appCode;
        ApplicationInfo applicationInfo= redisApplicationInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(applicationInfo)){
            applicationInfo = this.baseMapper.getAppInfoByCode(appCode);
            if(applicationInfo!=null) {
                redisApplicationInfoTemplate.opsForValue().set(redisKey, applicationInfo, APPINFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return applicationInfo;
    }

}
